package xzy.dao;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import xzy.pojo.Grade;
import xzy.pojo.PageBean;
import xzy.util.StringUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.Set;

@Repository("gradeMapper")
public class GradeMapperImpL implements GradeMapper {
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public Set<Grade> getGradeAll(Map<String,Integer> map) {
        return sqlSessionTemplate.getMapper(GradeMapper.class).getGradeAll(map);

    }

    public ResultSet getGradeAll1(PageBean pageBean,Grade grade) throws SQLException {

        StringBuffer sql=new StringBuffer("SELECT  * FROM grade");
        Connection connection=sqlSessionTemplate.getConfiguration().getEnvironment().getDataSource().getConnection();
        if(grade!=null&&StringUtil.isNotEmpty(grade.getGradeName())){
            sql.append(" and gradeName like '%"+grade.getGradeName()+"%'");
        }
        if(pageBean!=null){
            sql.append(" limit "+pageBean.getStart()+","+pageBean.getRows());
        }
        PreparedStatement psmt=connection.prepareStatement(sql.toString().replaceFirst("and", "where"));
        return psmt.executeQuery();


    }

    public int gradeCount() {
        return sqlSessionTemplate.getMapper(GradeMapper.class).gradeCount();
    }

    public int gradeDelete(String delIds) throws SQLException {
        StringBuffer sql=new StringBuffer("delete from grade where id in("+delIds+")");
        Connection connection=sqlSessionTemplate.getConfiguration().getEnvironment().getDataSource().getConnection();
        PreparedStatement psmt=connection.prepareStatement(sql.toString());
        return psmt.executeUpdate();
    }

    public int gradeAdd(Grade grade) {
        return sqlSessionTemplate.getMapper(GradeMapper.class).gradeAdd(grade);
    }

    public int gradeUpdate(Grade grade) {
        return sqlSessionTemplate.getMapper(GradeMapper.class).gradeUpdate(grade);
    }
}
